<template>
  <Dialog v-model="dialogVisible" width="500px" title="添加FTP" maxHeight="300px" @close="closeHandle">
    <el-form ref="ruleFormRef" :model="formData" :rules="formRules" label-width="80px" class="w-[400px]">
      <el-form-item label="用户名" prop="userName" required>
        <el-input
v-model="formData.userName" placeholder="请输入" clearable maxlength="120"
          @input="userNameChangeHandler" />
      </el-form-item>
      <el-form-item label="密码" prop="password" required>
        <el-input v-model="formData.password" placeholder="请输入" maxlength="120">
          <template #suffix>
            <Icon icon="ant-design:sync-outlined" class="cursor-pointer" :size="18" @click="getUuidHandle"></Icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="根目录" prop="ftpPath" required>
        <el-input v-model="formData.ftpPath" disabled placeholder="请输入" clearable maxlength="120" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeHandle">关闭</el-button>
      <el-button type="primary" :loading="loading" @click="saveHandle(ruleFormRef)">提交</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ref, computed, watch, defineEmits, reactive, onMounted } from 'vue'
import { setAddUserFTPAPI } from "@/api/systemMonitor"
import { propTypes } from '@/utils/propTypes'
import { randomPassword } from '@/utils/index'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['update:modelValue', 'confirm'])

const props = defineProps({
  modelValue: propTypes.bool.def(false),
  listIndex: propTypes.number,
  dataList: propTypes.array
})

//默认表单
const defaultFormData = {
  userName: "",
  ftpPath: "",
  password: "",
}

const loading = ref(false)

let formData = reactive({
  ...defaultFormData
})

const ruleFormRef = ref<FormInstance>()
const formRules = reactive<FormRules>({
  userName: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  ftpPath: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],

})
onMounted(() => {
  getUuidHandle();
})

//弹出框状态
const dialogVisible = ref(props.modelValue)

watch(() => props.modelValue, (newVal) => {
  dialogVisible.value = newVal;
})
//提交
const saveHandle = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      loading.value = true;
      setAddUserFTPAPI({
        ...formData,
      }).then(() => {
        ElMessage.success("添加成功")
        closeHandle()
        emit('confirm')
      }).finally(() => {
        loading.value = false;

      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
//关闭
const closeHandle = () => {
  dialogVisible.value = false;
  //初始化数据
  Object.assign(formData, defaultFormData);
  getUuidHandle();
  emit('update:modelValue', dialogVisible)
}
//获取随机的密码
const getUuidHandle = () => {
  formData.password = randomPassword(16)
}
//FTP路径默认值是`/duckup/ftp/{userName}`
const userNameChangeHandler = (value) => {
  formData.ftpPath = `/duckup/ftp/${value}`
}


</script>

<style lang="less" scoped>

</style>
